﻿using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace Homework16
{
    internal class Program
    {
        const string FILENAME = "D:\\CSharpHomework\\csharp2024\\Homework16\\Data.txt";
        /// <summary>
        /// No1：以1，Tom，30，生日（一定要是合法生日），身高，性别为例，AGE（20-60）之间的平均分布或者是以45为均值的正态分布，要求年龄在0-100之间，产生10000个。
        /// No2：数据分析，计算男女各自的平均年龄、平均身高、
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Random rnd = new Random(); // 创建随机数生成器实例
            List<DataEntry> dataEntries = new List<DataEntry>(); // 创建数据条目列表

            // 生成10000个数据条目
            for (int i = 0; i < 10000; i++)
            {
                int age = GenerateAge(rnd); // 生成年龄
                string name = GenerateName(rnd); // 生成姓名
                DateTime birthday = GenerateBirthday(age, rnd); // 根据年龄生成生日
                int height = GenerateHeight(rnd); // 生成身高
                string gender = GenerateGender(rnd); // 生成性别
                dataEntries.Add(new DataEntry // 添加数据条目到列表
                {
                    Id = i + 1,
                    Name = name,
                    Age = age,
                    Birthday = birthday,
                    Height = height,
                    Gender = gender
                });
            }

            WriteDataToFile(dataEntries); // 将数据写入文件
        }

        // 生成年龄，使用正态分布，均值为45，标准差为8
        static int GenerateAge(Random rnd)
        {
            double next = rnd.NextDouble();
            double mean = 45;
            double stdDev = 8;
            double result = mean + stdDev * Math.Sqrt(-2.0 * Math.Log(next)) * Math.Cos(2.0 * Math.PI * rnd.NextDouble());
            return ClampAge((int)Math.Round(result, MidpointRounding.AwayFromZero));
        }

        // 生成姓名，随机选择男性或女性名字
        static string GenerateName(Random rnd)
        {
            string[] firstNamesMale = { "John", "Bob", "Mike", "David", "William" };
            string[] firstNamesFemale = { "Jane", "Alice", "Emily", "Olivia", "Sophia" };
            string[] lastNames = { "Doe", "Smith", "Johnson", "Brown", "Davis" };
            string gender = GenerateGender(rnd);
            string firstName = gender == "Male" ? firstNamesMale[rnd.Next(firstNamesMale.Length)] : firstNamesFemale[rnd.Next(firstNamesFemale.Length)];
            string lastName = lastNames[rnd.Next(lastNames.Length)];
            return firstName + " " + lastName;
        }

        // 根据年龄生成生日，确保年龄和生日匹配
        static DateTime GenerateBirthday(int age, Random rnd)
        {
            DateTime currentDate = DateTime.Now;
            int year = currentDate.Year - age;
            int month = rnd.Next(1, 12);
            int day = rnd.Next(1, DateTime.DaysInMonth(year, month));
            return new DateTime(year, month, day);
        }

        // 生成身高，随机选择150到200之间的值，并转换为整数
        static int GenerateHeight(Random rnd)
        {
            return rnd.Next(150, 201); // 生成150到200之间的随机整数
        }

        // 生成性别，随机选择男性或女性
        static string GenerateGender(Random rnd)
        {
            return rnd.Next(0, 2) == 0 ? "Male" : "Female";
        }

        // 将年龄限制在0到100之间
        static int ClampAge(int age)
        {
            return Math.Max(0, Math.Min(age, 100));
        }

        // 将数据写入文件
        static void WriteDataToFile(List<DataEntry> dataEntries)
        {
            using (StreamWriter writer = new StreamWriter(FILENAME))
            {
                writer.WriteLine("Id,Name,Age,Birthday,Height,Gender");
                foreach (var entry in dataEntries)
                {
                    writer.WriteLine($"{entry.Id},{entry.Name},{entry.Age},{entry.Birthday.ToShortDateString()},{entry.Height},{entry.Gender}");
                }
            }
            Console.WriteLine("数据已成功写入Data.txt文件中");
        }

        class DataEntry
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
            public DateTime Birthday { get; set; }
            public int Height { get; set; } // 身高改为整数
            public string Gender { get; set; }
        }
    }
}

